package com.cxyzy.demo.property_anims

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.cxyzy.demo.R
import kotlinx.android.synthetic.main.activity_move.button1
import kotlinx.android.synthetic.main.activity_p_move.*


/**
 * 动画样例：移动控件
 */
class PMoveActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_p_move)
        initView()
    }

    private fun initView() {
        button1.setOnClickListener { move(it) }
        button2.setOnClickListener { move2(it) }
    }

    private fun move(srcView: View) {
        srcView.animate()
                .translationXBy(200f)
                .translationYBy(200f)
                .setDuration(500)
    }

    private fun move2(srcView: View) {
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(srcView, "translationX", 200f)
        animator.duration = 500
        animator.start()
    }

    private fun move3(srcView: View) {
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(srcView, "translationX", 200f)
        val animator2: ObjectAnimator = ObjectAnimator.ofFloat(srcView, "translationY", 200f)
        val animatorSet = AnimatorSet()
        animatorSet.duration = 500
        //同时执行多个动画
        animatorSet.playTogether(animator,animator2)
        //顺序执行多个动画
//        animatorSet.playSequentially(animator,animator2)
        animatorSet.start()
    }
}
